package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
   @PostMapping
   @ApiOperation("新增分类")
   public Result Insert(@RequestBody CategoryDTO categoryDTO)
    {
        log.info("新增分类，参数为{}",categoryDTO);
        categoryService.insert(categoryDTO);
        return  Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO)
    {
        log.info("分页查询，参数为{}",categoryPageQueryDTO);
        PageResult pageResult=categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("根据类型查询")
    @GetMapping("/list")
    public Result getByType(Integer type)
    {
        log.info("根据类型查询，参数为{}",type);
        List<Category> category=categoryService.getByType(type);
        return Result.success(category);
    }

    @ApiOperation("启用禁用分类")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,long id)
    {
        log.info("启用禁用分类，参数为{}，{}",status,id);
        categoryService.startOrStop(status,id);
        return Result.success();
    }

    @ApiOperation("编辑分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO)
    {
        log.info("修改分类，参数为{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

}
